import GoogleMobileAds

class NativeAdViewModel: NSObject, ObservableObject, GADNativeAdLoaderDelegate {
    @Published var nativeAd: GADNativeAd?
    @Published var isAdLoaded = false
    private var adLoader: GADAdLoader!
    var advalue  = ""
    var myAdSourceId = ""
    var myadID = ""
    var mycu = ""
    var myci = ""
    func refreshAd() {
        isAdLoaded = false  
        adLoader = GADAdLoader(
            adUnitID: nativeId,
            rootViewController: nil,
            adTypes: [.native], options: nil)
        adLoader.delegate = self
        adLoader.load(GADRequest())
    }

    func adLoader(_ adLoader: GADAdLoader, didReceive ad: GADNativeAd) {
        self.nativeAd = ad
        
        
        self.nativeAd?.paidEventHandler = { adValue in
                // 提取展示级广告收入数据
                let value = adValue.value
                
                let precision = adValue.precision
                let currencyCode = adValue.currencyCode
                
                // 获取广告单元 ID
               // let adUnitId = ad.adUnitID
                
                let responseInfo = ad.responseInfo
                let loadedAdNetworkResponseInfo = responseInfo.loadedAdNetworkResponseInfo
                let adSourceId = loadedAdNetworkResponseInfo?.adSourceID
                let adSourceInstanceId = loadedAdNetworkResponseInfo?.adSourceInstanceID
                let adSourceInstanceName = loadedAdNetworkResponseInfo?.adSourceInstanceName
                let adSourceName = loadedAdNetworkResponseInfo?.adSourceName
                let mediationGroupName = responseInfo.extrasDictionary["mediation_group_name"]
                let mediationABTestName = responseInfo.extrasDictionary["mediation_ab_test_name"]
                let mediationABTestVariant = responseInfo.extrasDictionary["mediation_ab_test_variant"]
            
            self.advalue  = "\(value)"
            self.myAdSourceId = "\(adSourceId ?? "null")"
            if let adSourceId = adSourceId {
                adLog(" 广告来源 ID: \(adSourceId)") // 输出广告来源 ID
                self.myadID = adSourceId

            }
            self.mycu = currencyCode
            self.myci = "\(value)"
            
          //logData(position: "native", status: "原生广告", uid: self.myadID, info: "原生广告价值", currency: self.mycu, cicros: self.myci)
            
            
            adLog("*****************原生广告数据***************************")
                // 输出获取的信息
                adLog(" 广告价值: \(value)") // 输出广告价值
                adLog(" 精度: \(precision)") // 输出广告收入的精度
                adLog(" 货币代码: \(currencyCode)") // 输出货币代码
               // adLog("xianban: 广告单元 ID: \(adUnitId)") // 输出广告单元 ID
                if let adSourceId = adSourceId {
                    adLog(" 广告来源 ID: \(adSourceId)") // 输出广告来源 ID
                }
                if let adSourceInstanceId = adSourceInstanceId {
                   // adLog("xianban: 广告来源实例 ID: \(adSourceInstanceId)") // 输出广告来源实例 ID
                }
                if let adSourceInstanceName = adSourceInstanceName {
                   // adLog("xianban: 广告来源实例名称: \(adSourceInstanceName)") // 输出广告来源实例名称
                }
                if let adSourceName = adSourceName {
                    //adLog("xianban: 广告来源名称: \(adSourceName)") // 输出广告来源名称
                }
                if let mediationGroupName = mediationGroupName {
                   // adLog("xianban: 中介组名称: \(mediationGroupName)") // 输出中介组名称
                }
                if let mediationABTestName = mediationABTestName {
                   // adLog("xianban: 中介 AB 测试名称: \(mediationABTestName)") // 输出中介 AB 测试名称
                }
                if let mediationABTestVariant = mediationABTestVariant {
                   // adLog("xianban: 中介 AB 测试变体: \(mediationABTestVariant)") // 输出中介 AB 测试变体
                }
            }
       
        
        ad.delegate = self
        isAdLoaded = true  // Set to true when ad is successfully loaded
        
      
    }

    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
        adLog("\(adLoader) failed with error: \(error.localizedDescription)")
        
      //logData(position: "native error", status: "native ad loag error", uid: self.myadID, info: "原生广告加载失败", currency: self.mycu, cicros: self.myci,errorInfo :" \(error.localizedDescription)")

        
        isAdLoaded = false  // Set to false when ad fails to load
    }
}

// MARK: - GADNativeAdDelegate implementation
extension NativeAdViewModel: GADNativeAdDelegate {
    func nativeAdDidRecordClick(_ nativeAd: GADNativeAd) {
        adLog("\(#function) called")
    }

    func nativeAdDidRecordImpression(_ nativeAd: GADNativeAd) {
        adLog("\(#function) called")
    }

    func nativeAdWillPresentScreen(_ nativeAd: GADNativeAd) {
        adLog("\(#function) called")
    }

    func nativeAdWillDismissScreen(_ nativeAd: GADNativeAd) {
        adLog("\(#function) called")
    }

    func nativeAdDidDismissScreen(_ nativeAd: GADNativeAd) {
        adLog("\(#function) called")
    }
}
